/**
 * 
 */
package info.niwota.fwrite;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

/**
 * @author qiangli
 *
 */
public class NoteListAdapter extends BaseAdapter {
	
	protected static class ViewHolder {
		public ImageView flag;
		public ImageView type;
		public FdocView content;
		public ImageView alert;
		public ImageView attach;
	}

	protected Context ctx;
	
	protected List<Document> notes;
	
	private boolean sortReverse = false;
	
	private int sortBy = Document.SORT_NONE;
	
	private Comparator<? super Document> comparator = new Comparator<Document>() {
		public int compare(Document d1, Document d2) {
			if (d1 == null) {
				return -1;
			}
			if (d2 == null) {
				return 1;
			}
			int sign = (sortReverse ? -1 : 1);
			int flag = sign * (int) (d2.getFlag() - d1.getFlag());
			int created = (int) (d1.getCreated() - d2.getCreated());
			
			if (sortBy == Document.SORT_FLAG) {
				return (flag != 0 ? flag : created);
			}
			
			int date = sign * (int) (d1.getModified() - d2.getModified());
			
			if (sortBy == Document.SORT_DATE) {
				return (date != 0 ? date : (flag != 0 ? flag : created));
			}
			return 0;
		}

	};

	public NoteListAdapter(Context ctx, List<Document> notes, int order) {
		this.ctx = ctx;
		this.notes = notes;
		this.sortBy = order;
		
		sort();
	}
	
	public NoteListAdapter(Context ctx, List<Document> notes) {
		this.ctx = ctx;
		this.notes = notes;
		this.sortBy = Document.SORT_NONE;
		//
		sort();
	}
	
	public void sort() {
		if (sortBy != Document.SORT_NONE && notes != null && notes.size() > 1) {
			Collections.sort(notes, comparator);
		}
	}

	public int getCount() {
		return notes == null ? 0 : notes.size();
	}

	public Document getItem(int pos) {
		return notes == null ? null : notes.get(pos);
	}

	public long getItemId(int pos) {
		return pos;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			LayoutInflater li = LayoutInflater.from(ctx);
			convertView = li.inflate(R.layout.note_list_item, null);
			holder = new ViewHolder();

			holder.flag = (ImageView) convertView.findViewById(R.id.note_item_flag);
			holder.type = (ImageView) convertView.findViewById(R.id.note_item_type);
			holder.content = (FdocView) convertView.findViewById(R.id.note_item_content);
			holder.alert = (ImageView) convertView.findViewById(R.id.note_item_alert);
			holder.attach = (ImageView) convertView.findViewById(R.id.note_item_attach);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Document item = getItem(position);
		
		if (item != null) {
			
			final int type = item.getType();
			
//			final boolean isTop = item.isTop();
//			if (isTop) {
//				holder.content.setText(ctx.getText(R.string.action_top));
//				holder.flag.setVisibility(View.VISIBLE);
//				holder.type.setVisibility(View.GONE);
//				holder.flag.setImageResource(R.drawable.top16);
//				holder.content.setBackgroundResource(R.drawable.background_black);
//			} 
			
			item.show(holder.content);
			holder.flag.setVisibility(View.VISIBLE);
			holder.type.setVisibility(View.VISIBLE);
			holder.type.setImageResource(type == Document.TYPE_LIST ? R.drawable.type_list16 : R.drawable.empty);
			holder.flag.setImageResource(item.getFlagIcon());
			holder.content.setBackgroundResource(item.getBackground());
			
			if (item.hasAlert()) {
				holder.alert.setVisibility(View.VISIBLE);
				holder.alert.setImageResource(R.drawable.alarm16);
			} else {
				holder.alert.setVisibility(View.GONE);
			}
			if (item.hasAttachment()) {
				holder.attach.setVisibility(View.VISIBLE);
				holder.attach.setImageResource(R.drawable.attach16);
			} else {
				holder.attach.setVisibility(View.GONE);
			}
		} else {
			holder.content.setBackgroundResource(R.drawable.background_black);
		}
		
		return convertView;
	}

	public void add(Document doc) {
		notes.add(doc);
		notifyDataSetChanged();
	}
	
	public void setAll(List<Document> docs) {
		notes.clear();
		notes.addAll(docs);
		notifyDataSetChanged();
	}

	public void delete(Document doc) {
		notes.remove(doc);
		notifyDataSetChanged();
	}

	public void order(int by) {
		sortReverse = !sortReverse && (sortBy == by);
		sortBy = by;
		if (notes != null && notes.size() > 1) {
			Collections.sort(notes, comparator);
			notifyDataSetChanged();
		}
	}
	
	/**
	 * reorder due to flag change
	 */
	public void order() {
		if (notes != null && notes.size() > 1) {
			Collections.sort(notes, comparator);
		}
		//must notify change to update flag even if nor order change
		notifyDataSetChanged();
	}

	public int getSortIcon() {
		if (sortBy == Document.SORT_FLAG && !sortReverse) {
			return R.drawable.sort_flag22;
		}
		if (sortBy == Document.SORT_FLAG && sortReverse) {
			return R.drawable.sort_flag22b;
		}
		if (sortBy == Document.SORT_DATE && !sortReverse) {
			return R.drawable.sort_date22;
		}
		if (sortBy == Document.SORT_DATE && sortReverse) {
			return R.drawable.sort_date22b;
		}
		//
		return R.drawable.sort_flag22;
	}

	public int getSortBy() {
		return sortBy;
	}
}
